/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 12, 2012  10:41:08 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : SyncSequenceServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.MmVendorMasInb;
import jpa.entitys.synchro.staging.SyncSequence;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class SyncSequenceServices extends JPAService {
	
	
	private static final Logger log = Logger.getLogger(SyncSequenceServices.class);
	/**
	 * @param em
	 */
	public SyncSequenceServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public SyncSequence findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "SyncSequence.findById"));				
	}

	public SyncSequence Inserted(SyncSequence vlaue) throws Exception {
		SyncSequence obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getSyncSequenceId()))
			{
				String id = super.SequenceNext("SYNC_SEQUENCE");					
				obj.setSyncSequenceId(id);
				obj.setCreatedBy(this.getUser());
				obj.setUpdatedBy(this.getUser());				
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setSyncIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	
	public List<SyncSequence> Inserted(List<SyncSequence> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SyncSequence obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getSyncSequenceId()))
				{
					String id = super.SequenceNext("SYNC_SEQUENCE");					
					obj.setSyncSequenceId(id);
					obj.setCreatedBy(this.getUser());
					obj.setUpdatedBy(this.getUser());				
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setSyncIssync("N");
					common_lst.add(obj);
				}	
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	public List<SyncSequence> Updated(List<SyncSequence> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SyncSequence obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getSyncSequenceId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
}
